﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using View.Control.Modules;
using DevExpress.DXperience.Demos;
using DevExpress.XtraSplashScreen;
using TestMySqlConnetPoll;
using System.Threading;
using View.Common;
using DevExpress.XtraBars;

namespace View.Statistics
{
    /// <summary>
    /// 统计报表
    /// </summary>
    public partial class FormStatistics : DevExpress.XtraEditors.XtraForm
    {
        private TongJiTable tongjiT = new TongJiTable();
        private List<View.Data.Data> source = new List<View.Data.Data>();
        private List<View.Data.Data> source_chart = new List<View.Data.Data>();
        private string NowDashboard = string.Empty;
        private string strNowSelTime = string.Empty;

        public FormStatistics()
        {
            InitializeComponent();
           
            this.barItemType.EditValue = this.comboBox_type.Items[0];

            //NowDashboard = "信号机开放次数统计";
            //GetData();
        }

        /// <summary>
        /// 初始化统计类型数据
        /// </summary>
        /// <param name="strTjName"></param>
        private void IniStatisticsSource(string strTjName, int year)
        {
            this.source.Clear();
            if (!c_Device.TongJiConfig.ContainsKey(strTjName))
                return;

            switch (NowDashboard)
            {
                case "信号机开放次数统计":
                case "破封按钮运用次数统计":
                case "区段占用次数统计":
                case "列调按钮运用次数统计":
                case "故障通知按钮运用次数统计":
                    foreach (objtjDev dev in c_Device.TongJiConfig[strTjName])
                    {
                        for (int i = 1; i <= 12; i++)
                        {
                            View.Data.Data data = new Data.Data();
                            data.Category = dev.DevName;
                            GetStaticDataList(data, i, new objTongJiReport());
                            data.DevNo = dev.devNo;
                            data.Year = year.ToString();
                            data.Date = new DateTime(year, i, 1);

                            this.source.Add(data);
                        }
                    }
                    break;
                case "道岔动作次数统计":
                case "设备故障次数统计":
                    source_chart.Clear();
                    foreach (objtjDev dev in c_Device.TongJiConfig[strTjName])
                    {
                        View.Data.Data data = new Data.Data();
                        data.Category = dev.DevName;
                        GetStaticDataList(data, 0, new objTongJiReport());
                        data.DevNo = dev.devNo;
                        data.Year = year.ToString();
                        data.Date = new DateTime(year, 1, 1);

                        this.source.Add(data);
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 获取Dashboard数据
        /// </summary>
        /// <returns></returns>
        private void GetData()
        {
            if ((string)barItemTime.EditValue == null || (string)barItemTime.EditValue == "")
            {
                IniStatisticsSource(NowDashboard, 2014);
                return;
            }

            tongjiT.ReadValueList(NowDashboard, DateTime.Parse(this.barItemTime.EditValue.ToString()).Year, 0);

            while (!tongjiT.Ready_ValueList)
            {
                if (tongjiT.ErrorFlag != 0)
                {
                    MessageBox.Show("查询不到对应数据");
                    return;
                }

                Thread.Sleep(20);
            }

            IniStatisticsSource(NowDashboard, DateTime.Parse(this.barItemTime.EditValue.ToString()).Year);

            //key.type
            //foreach (objTongJiKey key in tongjiT.ValueList.Keys)
            //{
            //    foreach (objTongJiReport report in tongjiT.ValueList[key])
            //    {
            //        View.Data.Data data = source.Find(s => s.Category == key.DevName);
            //        if (data != null)
            //        {
            //            //report.
            //            data.Year = report.year.ToString();
            //            GetStaticDataList(data, report.month, report);

            //            data.Date = new DateTime(report.year, report.month, 1);
            //        }
            //    }
            //}

            switch (NowDashboard)
            {
                case "信号机开放次数统计":
                case "破封按钮运用次数统计":
                case "区段占用次数统计":
                case "列调按钮运用次数统计":
                case "故障通知按钮运用次数统计":
                    foreach (objTongJiKey key in tongjiT.ValueList.Keys)
                    {
                        foreach (objTongJiReport report in tongjiT.ValueList[key])
                        {
                            View.Data.Data data = source.Find(s => (s.Category == key.DevName && s.Date.Month == report.month));
                            if (data != null)
                            {
                                //report.
                                data.Year = report.year.ToString();
                                GetStaticDataList(data, report.month, report);

                                data.Date = new DateTime(report.year, report.month, 1);
                            }
                        }
                    }
                    break;
                case "道岔动作次数统计":
                case "设备故障次数统计":
                    foreach (objTongJiKey key in tongjiT.ValueList.Keys)
                    {
                        foreach (objTongJiReport report in tongjiT.ValueList[key])
                        {
                            View.Data.Data data = source.Find(s => s.Category == key.DevName);
                            if (data != null)
                            {
                                //report.
                                data.Year = report.year.ToString();
                                GetStaticDataList(data, report.month, report);

                                //data.Date = new DateTime(report.year, report.month, 1);
                            }
                        }
                    }

                    foreach (View.Data.Data data in source)
                    {
                        data.Frequency = data.Frequency1 + data.Frequency2 + data.Frequency3 + data.Frequency4
                            + data.Frequency5 + data.Frequency6 + data.Frequency7 + data.Frequency8
                             + data.Frequency9 + data.Frequency10 + data.Frequency11 + data.Frequency12;
                    }

                    List<objTongJiReport> objList = tongjiT.GetDevCount(NowDashboard, DateTime.Parse(this.barItemTime.EditValue.ToString()).Year);
                    foreach (objTongJiReport report in objList)
                    {
                        View.Data.Data data = new Data.Data();
                        data.Date = new DateTime(report.year, report.month, 1);
                        data.Frequency = report.totalCount;
                        data.Duration = report.totolTime;
                        this.source_chart.Add(data);
                    }

                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// 设置月份
        /// </summary>
        /// <param name="data"></param>
        /// <param name="month"></param>
        public void GetStaticDataList(View.Data.Data data , int month,objTongJiReport report)
        {
            switch (month)
            {
                case 1:
                    data.Month = "一月";
                    data.Frequency1 = report.totalCount;
                    break;
                case 2:
                    data.Month = "二月";
                    data.Frequency2 = report.totalCount;
                    break;
                case 3:
                    data.Month = "三月";
                    data.Frequency3 = report.totalCount;
                    break;
                case 4:
                    data.Month = "四月";
                    data.Frequency4 = report.totalCount;
                    break;
                case 5:
                    data.Month = "五月";
                    data.Frequency5 = report.totalCount;
                    break;
                case 6:
                    data.Month = "六月";
                    data.Frequency6 = report.totalCount;
                    break;
                case 7:
                    data.Month = "七月";
                    data.Frequency7 = report.totalCount;
                    break;
                case 8:
                    data.Month = "八月";
                    data.Frequency8 = report.totalCount;
                    break;
                case 9:
                    data.Month = "九月";
                    data.Frequency9 = report.totalCount;
                    break;
                case 10:
                    data.Month = "十月";
                    data.Frequency10 = report.totalCount;
                    break;
                case 11:
                    data.Month = "十一月";
                    data.Frequency11 = report.totalCount;
                    break;
                case 12:
                    data.Month = "十二月";
                    data.Frequency12 = report.totalCount;
                    break;
                default:
                    break;

            }

            switch (NowDashboard)
            {
                case "信号机开放次数统计":
                case "破封按钮运用次数统计":
                case "区段占用次数统计":
                case "列调按钮运用次数统计":
                case "故障通知按钮运用次数统计":
                    data.Frequency = report.totalCount;
                    data.Duration = report.totolTime;
                    break;
                case "道岔动作次数统计":
                case "设备故障次数统计":
                    
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 生成时间列表
        /// </summary>
        private void GenTimeList()
        {
            comboBox_time.Items.Clear();
            //this.barItemTime.EditValue = "";
            tongjiT.ReadTimeList((string)barItemType.EditValue);

            while (!tongjiT.Ready_TimeList)
            {
                if (tongjiT.ErrorFlag != 0)
                {
                    MessageBox.Show("查询不到对应时间列表");
                    return;
                }

                Thread.Sleep(20);
            }



            foreach (int dt in tongjiT.TimeList)
            {
                comboBox_time.Items.Add(dt.ToString() + "年");
            }

            if (comboBox_time.Items.Count > 0)
                this.barItemTime.EditValue = comboBox_time.Items[0];
            else
                this.barItemTime.EditValue = "";
        }


        /// <summary>
        /// 重新加载Dashboard
        /// </summary>
        /// <param name="strDashboard"></param>
        private void ReloadDashboard(string strDashboard)
        {
            switch (strDashboard)
            {
                case "信号机开放次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_xhj.xml");
                    break;
                case "破封按钮运用次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_pf.xml");
                    break;
                case "区段占用次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_qd.xml");
                    break;
                case "列调按钮运用次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_ld.xml");
                    break;
                case "故障通知按钮运用次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_gz.xml");
                    break;
                case "道岔动作次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_dc.xml");
                    break;
                case "设备故障次数统计":
                    this.dashboardViewer1.LoadDashboard(Application.StartupPath + "\\dashboard\\GridChart_sb.xml");
                    break;
                default:
                    break;
            }

            
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DashboardViewer_DataLoading(object sender, DevExpress.DataAccess.DataLoadingEventArgs e)
        {
            if (e.DataSourceName == "Default Datasource")
                e.Data = this.source;
            else if (e.DataSourceName == "Default Datasource1")
                e.Data = this.source_chart;

            if (NowDashboard == string.Empty)
                return;
        }

        /// <summary>
        /// 时间改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Time_EditValueChanged(object sender, EventArgs e)
        {
            GetData();
            this.dashboardViewer1.ReloadData();
        }

        /// <summary>
        /// 统计类型改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Type_EditValueChanged(object sender, EventArgs e)
        {
            if (NowDashboard == this.barItemType.EditValue as string) return;
            NowDashboard = this.barItemType.EditValue as string;
            this.Text = NowDashboard;
            ReloadDashboard(NowDashboard);
            GenTimeList();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {

        }
    }
}